<template>
  <div class="app-container">
    <el-form
      size="small"
      :inline="true"
      label-position="left"
      label-width="90px"
    >
      <el-form-item label="单位名称：">
        <div class="fw-7 color3">{{ cname }}</div>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="companyList">
      <el-table-column
        label="ID"
        align="center"
        prop="id"
        fixed="left"
        width="80"
      />
      <el-table-column label="服务名称" align="center" prop="serviceName" />
      <el-table-column label="简介" align="center" prop="introductions" />
      <el-table-column label="图标" align="center" prop="iconUrl" width="100">
        <template slot-scope="scope">
          <image-preview
            v-if="scope.row.iconUrl"
            :src="scope.row.iconUrl"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="链接" align="center" prop="url">
        <template slot-scope="{ row }">
          <el-link :href="row.url" type="success" target="_blank">{{
            row.url
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        prop="cname"
        fixed="right"
        width="120"
      >
        <template slot-scope="{ row }">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="80px">
        <el-form-item label="栏目名称" prop="serviceName">
          <el-input
            v-model="form.serviceName"
            placeholder="请输入栏目名称"
            :maxlength="10"
          />
        </el-form-item>
        <el-form-item label="栏目简介" prop="introductions">
          <el-input
            v-model="form.introductions"
            placeholder="请输入栏目简介"
            :maxlength="10"
          />
        </el-form-item>
        <el-form-item label="链接地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入链接地址" />
        </el-form-item>
        <el-form-item label="图标上传" prop="iconUrl">
          <ImageUpload v-model="form.iconUrl" :fileSize="1" :width="50" :height="50" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCompany,
  companyHomeList,
  companyAddHome,
  companyEditHome,
  companyDeleteHome,
  companyDetailHome,
} from "@/api/company/company";
import ImageUpload from '@/components/ImageUpload'

export default {
  name: "CompanyHome",
  components: {
    ImageUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      cname: "", // 單位名稱
      companyList: [],
      title: "",
      open: false,
      form: {},
      rules: {},
    };
  },
  created() {
    const companyId = this.$route.params && this.$route.params.companyId;
    this.getCompanyName(companyId);
    this.getList();
  },
  methods: {
    getCompanyName(companyId) {
      getCompany(companyId).then((res) => {
        this.cname = res.data.cname;
      });
    },
    getList() {
      this.loading = true;
      companyHomeList(this.$route.params.companyId).then((res) => {
        this.companyList = res.data;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        comId: "",
        serviceName: "",
        introductions: "",
        url: "",
        iconUrl: "",
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务";
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.comId = this.$route.params && this.$route.params.companyId;
          if (this.form.id != null) {
            companyEditHome(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            companyAddHome(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = "编辑服务";
      this.form = row
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
     /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除服务名称为"' + row.serviceName + '"的数据项？').then(function () {
          return companyDeleteHome(row.id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>

<style>
</style>
